package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsCategoryService;
import com.qf.vo.CatVo;
import com.qf.vo.CategorySellAmts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.swing.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DtsCategoryServiceImpl implements DtsCategoryService {

    @Autowired
    StatMapper statMapper;

    @Autowired
    DtsCategoryMapper categoryMapper;

    @Override
    public List<CategorySellAmts> findCategorySell() {
        return statMapper.findCategorySell();
    }

    @Override
    public List<CatVo> queryCategoryList() {
        //分两步查询，先查询出一级目录，在查询出一级目录对应的二级目录
        LambdaQueryWrapper<DtsCategory> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(DtsCategory::getPid,0);
        queryWrapper1.eq(DtsCategory::getDeleted,false);
        List<DtsCategory> categories = categoryMapper.selectList(queryWrapper1);
        List<CatVo> catVos = categories.stream().map(categories1 -> {
            CatVo catVo1 = new CatVo();
            catVo1.setValue(categories1.getId());
            catVo1.setLabel(categories1.getName());

            LambdaQueryWrapper<DtsCategory> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(DtsCategory::getPid, categories1.getId());
            queryWrapper2.eq(DtsCategory::getDeleted, false);
            List<DtsCategory> dtsCategories = categoryMapper.selectList(queryWrapper2);
            List<CatVo> collect = dtsCategories.stream().map(categories2 -> {
                CatVo catVo2 = new CatVo();
                catVo2.setValue(categories2.getId());
                catVo2.setLabel(categories2.getName());
                return catVo2;
            }).collect(Collectors.toList());
            catVo1.setChildren(collect);
            return catVo1;
        }).collect(Collectors.toList());
        return catVos;
    }

    @Override
    public IPage<DtsCategory> queryPage(Integer page, Integer limit, String sort, String order, Integer id, String name) {
        IPage<DtsCategory> iPage = new Page<>(page,limit);
        QueryWrapper<DtsCategory> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("desc")){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }

        if(!StringUtils.isEmpty(id)){
            qw.like("id",id);
        }
        if(!StringUtils.isEmpty(name)){
            qw.like("name",name);
        }
        qw.eq("deleted",false);
        categoryMapper.selectPage(iPage,qw);
        return iPage;
    }

    @Override
    public List<Map<String, Object>> queryL1() {
        LambdaQueryWrapper<DtsCategory> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsCategory::getLevel,"L1");
        List<DtsCategory> dtsCategories = categoryMapper.selectList(qw);

        List<Map<String, Object>> collect = dtsCategories.stream().map(categories -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", categories.getId());
            map.put("label", categories.getName());
            return map;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public DtsCategory insert(DtsCategory category) {
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
        category.setDeleted(false);
        categoryMapper.insert(category);
        return category;
    }

    @Override
    public void update(DtsCategory category) {
        category.setUpdateTime(new Date());
        categoryMapper.updateById(category);
    }

    @Override
    public void delete(DtsCategory category) {
        //还应判断一下该类目中是否还有商品关联
        category.setDeleted(true);
        categoryMapper.updateById(category);
    }
}
